/**
 * Created by shidaiapp on 2017/9/29.
 */


function changeModelLoading(self, tiyData) {
  tiyData.tStatus.modalDeliver.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalDeliver.isLoading = true;
  });
}
/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParams.orderSerialNumber = '';
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.querySearchKeywords({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage,
    "keyword":tiyData.tParams.searchParams.keyword,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tData.searchKeywordList.length = 0;
      let searchKeywordPage = response.result;
      tiyData.tPage.total = searchKeywordPage.total;
      tiyData.tPage.stepNumber = searchKeywordPage.pageSize;
      tiyData.tData.searchKeywordList.splice(0, tiyData.tData.searchKeywordList.length);
      searchKeywordPage.list.forEach(
        function (item) {
          tiyData.tData.searchKeywordList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 更新缺货退款状态
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webAddEditSearchKeyword = (self, tiyData) => {
  if (tiyData.tParams.searchKeyword.keyword == ""){
    self.$Message.error({
      content:"关键字不能为空"
    });
    return;
  }
  self.$Spin.show();
  self.$apiOther.addEditSearchKeyword({
    "id": tiyData.tParams.searchKeyword.id,
    "keyword": tiyData.tParams.searchKeyword.keyword,
    "automatedWords":tiyData.tParams.searchKeyword.automatedWords,
    "synonym":tiyData.tParams.searchKeyword.synonym,
    "isFuzzy":tiyData.tParams.searchKeyword.isFuzzy,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('保存成功!');
      tiyData.tStatus.modalAddEdit.isShow = false;
      webList(self,tiyData)
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 更新缺货退款状态
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const webDeleteKeyword = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.deleteSearchKeyword({
    "id": tiyData.tParams.deleteKeyword.id,
    "keyword": tiyData.tParams.deleteKeyword.keyword,
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.success('删除成功!');
      tiyData.tStatus.modalDelete.isShow = false;
      webList(self,tiyData);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
